
<template>
  <div class="CardContainer">
    <el-card header="登录界面">
      <el-form
        :model="FromData"
        status-icon
        :rules="rules"
        ref="FromData"
        label-width="100px"
        @keyup.enter.native="submitForm('FromData')"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="FromData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="FromData.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('FromData')"
            >登录</el-button
          >
          <el-button @click="resetForm">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { Login } from "../api/users";
import { setToken } from "../Utils/auth";
export default {
  data() {
    return {
      FromData: {
        username: "",
        password: "",
      },
      //非空验证 prop
      rules: {
        username: [
          { required: true, message: "用户名不能为空!", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空!", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //登录验证 用户名密码是否正确
          Login(this.FromData)
            .then((data) => {
              // console.log(data);
              //如果正确就将token 和 refreshToken 加入到localStorage中保存 并且跳转到首页
              if (data.data.code === 1000) {
                var res = data.data.data;
                //保存token 和 refreshToken and 用户名
                setToken(res.token, res.refreshToken, res.username, res.userid);
                this.$message({
                  showClose: true,
                  message: data.data.msg,
                  type: "success",
                  center: true,
                });
                this.$router.push("/");
              } else {
                //错误提示错误信息
                this.$message({
                  showClose: true,
                  message: "用户名或密码错误,请重新输入！",
                  type: "error",
                  center: true,
                });
              }
            })
            .catch((err) => {
              this.$message({
                showClose: true,
                message: err,
                type: "error",
                center: true,
              });
            });
        } else {
          this.$message({
            showClose: true,
            message: "用户名或密码不能为空,请重新输入！",
            type: "error",
            center: true,
          });
          return false;
        }
      });
    },
    resetForm() {
      this.$router.push("/register");
    },
  },
};
</script>
<style scoped>
.el-card.is-always-shadow {
  width: 450px;
}
.CardContainer {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
body {
  margin: 0px;
  background-color: #eeee;
}
.el-card__body {
  width: 400px;
}
.el-card__header {
  text-align: center;
}
</style>